<?php

namespace App\Http\Controllers;

use App\Models\UserRequirement;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\UserInfo;
use Carbon\Carbon;
use \Cache;
use \Auth;
use \Schema;

class UserInfoController extends Controller
{

    protected $ignoreParameters = ['created_at', 'updated_at', 'user_id'];


    /**
     * 设置个人信息
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     * @throws \Illuminate\Validation\ValidationException
     */
    public function setInfo(Request $request)
    {
        $this->validate($request,[
            'head_image'=>'nullable|string',
            'album'=>'nullable|string',
            'nickname'=>'nullable|string',
            'sex'=>'nullable|numeric',
            'birth_day'=>'nullable|date',
            'height'=>'nullable|numeric',
            'weight'=>'nullable|numeric',
            'income'=>'nullable|string',
            'province'=>'nullable|string',
            'city'=>'nullable|string',
            'county'=>'nullable|string',
            'address'=>'nullable|string',
            'education'=>'nullable|string',
            'if_married'=>'nullable|numeric',
            'if_child'=>'nullable|numeric',
            'if_want_child'=>'nullable|numeric',
            'occupation'=>'nullable|string',
            'house'=>'nullable|string',
            'car'=>'nullable|numeric',
            'smoke'=>'nullable|numeric',
            'drink'=>'nullable|numeric',
            'come_from'=>'nullable|string',
            'shape'=>'nullable|string',
            'constellation'=>'nullable|string',
            'nation'=>'nullable|string',
            'when_married'=>'nullable|string',
            'soliloquy'=>'nullable|string',
            'marks'=>'nullable|string',
            'vip'=>'nullable|string',
            'secret_info'=>'nullable|string',

        ]);
        $inputs = $request->input();
        $user = User::where('id',Auth::user()->id)->first();
        if( empty($user) )
        {
            return response('用户不存在', 403);
        }
        $userInfo = UserInfo::firstOrCreate(['user_id' => Auth::user()->id]);
        $filters = Schema::getColumnListing('user_infos');
        $ignores = $this->ignoreParameters;
        $infoParams = array_filter($inputs, function ($key) use ($filters, $ignores) {
            return in_array($key, $filters) && !in_array($key, $ignores);
        }, ARRAY_FILTER_USE_KEY);
        $result = UserInfo::where(['user_id' => Auth::user()->id])->update($infoParams);
        $requirement = UserRequirement::where(['user_id' => Auth::user()->id])->first();
        if(empty($requirement)){
            UserRequirement::create([
                'user_id'=>Auth::user()->id,
            ]);
        }
        return $this->successReturn($result);

    }

    public function selectData()
    {
        $whitelist = \Storage::get('jsons/LoveData.json');
        return $this->successReturn(json_decode($whitelist , true));
    }

}
